﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AllenalaMente.Games.Flux
{
    public enum TileType
    {
        Empty,
        Beam,
        Mirror
    }

    public partial class Tile : UserControl
    {
        public Coordinates Coordinates;
        public int correctAngle = 0;
        public TileType Type;
        public bool IsTouched = false;

        public Tile(string floorImagePath, Coordinates coords)
        {
            InitializeComponent();

            this.Coordinates = coords;

            // Prepare floor image
            Uri uri = new Uri(floorImagePath, UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
            imageFloorTile.SetValue(Image.SourceProperty, img);
        }

        public Tile()
        {
            InitializeComponent();
        }

        public void ShowMirror(int angle)
        {
            imageMirror.Visibility = System.Windows.Visibility.Visible;
            rotateTransform.Angle = angle;
        }

        public void Resize(int width, int height)
        {
            Width = width;
            Height = height;
            imageFloorTile.Width = width;
            imageFloorTile.Height = height;
            imageMirror.Width = width;
            imageMirror.Height = height;
            rotateTransform.CenterX = width / 2;
            rotateTransform.CenterY = height / 2;
        }

        public bool IsRotationCorrect()
        {
            return rotateTransform.Angle == correctAngle;
        }

        public void HideBeams()
        {
            canvasBeams.Children.Clear();
        }

        public void DrawBeam(Direction from, Direction to, Color color)
        {
            // Raggio d'ingresso
            Rectangle rectangle1 = new Rectangle();
            if (from == Direction.Left || from == Direction.Right)
            {
                rectangle1.Width = 27;
                rectangle1.Height = 4;
                Canvas.SetTop(rectangle1, (imageFloorTile.Height / 2) - 2);
                if (from == Direction.Right)
                    Canvas.SetLeft(rectangle1, imageFloorTile.Width / 2);
            }
            else
            {
                rectangle1.Height = 25;
                rectangle1.Width = 4;
                Canvas.SetLeft(rectangle1, (imageFloorTile.Width / 2) - 2);
                if (from == Direction.Down)
                    Canvas.SetTop(rectangle1, imageFloorTile.Height / 2);
            }
            rectangle1.Fill = new SolidColorBrush(color);
            canvasBeams.Children.Add(rectangle1);

            // Raggio d'uscita
            Rectangle rectangle2 = new Rectangle();
            if (to == Direction.Left || to == Direction.Right)
            {
                rectangle2.Width = 27;
                rectangle2.Height = 4;
                Canvas.SetTop(rectangle2, (imageFloorTile.Height / 2) - 2);
                if (to == Direction.Right)
                    Canvas.SetLeft(rectangle2, imageFloorTile.Width / 2);
            }
            else
            {
                rectangle2.Height = 25;
                rectangle2.Width = 4;
                Canvas.SetLeft(rectangle2, (imageFloorTile.Width / 2) - 2);
                if (to == Direction.Down)
                    Canvas.SetTop(rectangle2, imageFloorTile.Height / 2);
            }
            rectangle2.Fill = new SolidColorBrush(color);
            canvasBeams.Children.Add(rectangle2);
        }

        private void imageFloorTile_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            rotateTransform.Angle += 90;
            if (rotateTransform.Angle == 360)
                rotateTransform.Angle = 0;
        }
    }
}
